package com.leyou.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.LyExceptionEnums;
import com.leyou.common.exception.LyException;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.LyBaseMapper;
import com.leyou.item.mapper.lymapper.BrandMapper;
import com.leyou.item.service.base.AbstractBaseService;
import com.leyou.item.service.BrandService;
import com.leyou.item.vo.PageResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.object.UpdatableSqlQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author linzx
 * @date 2020/3/24
 */
@Service
public class BrandServicImpl extends AbstractBaseService<Brand> implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    protected LyBaseMapper<Brand> getMapper() {
        return brandMapper;
    }

    @Override
    public PageResult<Brand> queryBrandByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        PageHelper.startPage(page,rows);
        Example brandExample = new Example(Brand.class);
        if (StringUtils.isNotBlank(key)) {
            brandExample.createCriteria().orLike("name", "%" + key + "%").orLike("letter", key.toUpperCase());
        }
        if (StringUtils.isNotBlank(sortBy)) {
            String orderByClause = sortBy + (desc ? " desc" : " asc");
            brandExample.setOrderByClause(orderByClause);
        }
        List<Brand> brandList = this.selectByExample(brandExample);

        PageInfo<Brand> brandPageInfo = new PageInfo<>(brandList);

        return new PageResult<>(brandPageInfo.getTotal(),brandList);
    }

    @Override
    @Transactional
    public int insertCategoryBrand(Long cid, Long bid) {
        int effect = brandMapper.insertCategoryBrand(cid, bid);
        if (effect != 1) {
            throw new LyException(LyExceptionEnums.BRAND_CREATE_FAILED);
        }
        return effect;
    }



    @Override
    public void InserBrandAndCategoryId(Brand brand ,  List<Long> cids) {
        brand.setId(null);
        int i = insertSelective(brand);
        if (i != (cids.size() + 1)) {
            throw new LyException(LyExceptionEnums.BRAND_NOT_FOUND);
        }
    }

    /**
     * 品牌的修改
     * 其实就是先删除中间表的内容后添加上
     *
     * @param brand
     */
    @Transactional
    @Override
    public void updateBrand(Brand brand, List<Long> cids) {
        updateByPrimaryKeySelective(brand);
        brandMapper.deleteCategoryBrandByBrandId(brand.getId());
     cids.forEach(cid -> insertCategoryBrand(cid,brand.getId()));
    }

    /**
     * 通过cid查找brand信息
     * @param cid
     * @return
     */
    @Override
    public List<Brand> findBrandByCid(Long cid) {
        List<Brand> brandList = brandMapper.findBrandByCid(cid);
        if (brandList == null) {
            throw new LyException(LyExceptionEnums.BRAND_NOT_FOUND);
        }
        return brandList;
    }
}
